package com.tanger.permission.permission.web;

import com.tanger.common.exception.AbstractController;
import com.tanger.common.result.ActionResult;
import com.tanger.common.utils.StringUtil;
import com.tanger.permission.entity.permission.EntityRolePermission;
import com.tanger.permission.entity.vo.RolePermissionVo;
import com.tanger.permission.permission.service.RolePermissionService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;

/**
 * 控制层
 */
@RestController
@RequestMapping("/tanger/bts/role-permission")
@Api(value = "角色权限",tags="角色权限")
public class RolePermissionController extends AbstractController {

	@Autowired
	private RolePermissionService rolePermissionService;

	private final Logger logger = LoggerFactory.getLogger(getClass());

	@ApiImplicitParam(name = "roleId", value = "主键ID", required = true, dataType = "Long")
	@ApiOperation(value = "角色权限详情")
	@GetMapping("/{roleId}")
	public ActionResult<List<RolePermissionVo>> getInfo(@PathVariable Long roleId) {
		try {
			if (StringUtil.isNull(roleId)) return ActionResult.buildResultError("参数缺失");
			return ActionResult.buildResultSuccess(rolePermissionService.RolepermissionInfo(roleId));
		} catch (Exception e) {
			return ActionResult.buildResultError(e.getMessage());
		}
	}

	@ApiImplicitParam(name = "roleId", value = "主键ID", required = true, dataType = "Long")
	@ApiOperation(value = "保存角色权限")
	@PostMapping(value = "/saveRolePermission/{roleId}")
	public ActionResult saveRolePermission(@PathVariable Long roleId,@RequestBody List<EntityRolePermission> list) {
		try {
			if (CollectionUtils.isEmpty(list)) return ActionResult.buildResultSuccess();
			long count = list.stream().filter(x -> StringUtil.isNull(x.getPermissionId()) || StringUtil.isNull(x.getRoleId())).count();
			if (StringUtil.isNull(roleId) || count > 0L) return ActionResult.buildResultError("参数缺失");
			return rolePermissionService.savaRolePermission(roleId,list);
		} catch (Exception e) {
			return ActionResult.buildResultError(e.getMessage());
		}
	}

	@ApiImplicitParam(name = "ids", value = "主键ID数组", required = true, dataType = "Long")
	@ApiOperation(value = "批量角色权限删除")
	@PostMapping(value = "/deleteById")
	public ActionResult deleteById(@RequestBody Long[] ids) {
		try {
			if (CollectionUtils.isEmpty(Arrays.asList(ids))) return  ActionResult.buildResultError("参数缺失");
			return rolePermissionService.deleteRolePermission(ids);
		} catch (Exception e) {
			return ActionResult.buildResultError(e.getMessage());
		}
	}

	@ApiImplicitParam(name = "roleId", value = "主键ID", required = true, dataType = "Long")
	@ApiOperation(value = "清空角色权限删除")
	@PostMapping(value = "/deleteByRoleId/{roleId}")
	public ActionResult deleteByRoleId(@PathVariable Long roleId) {
		try {
			if (StringUtil.isNull(roleId)) return  ActionResult.buildResultError("参数缺失");
			return rolePermissionService.deleteRolePermissionByRoleId(roleId);
		} catch (Exception e) {
			return ActionResult.buildResultError(e.getMessage());
		}
	}
}